import Vue from "vue";
import axios from "axios";
import QS from "qs";
import { Toast } from "vant";

// Vue.use(Toast);
// 默认超时设置
axios.defaults.timeout = 50000;
//配置请求头
axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded;charset=UTF-8";
// 相对路径设置
axios.defaults.baseURL = "http://httpbin.org";

//POST传参序列化(添加请求拦截器)
axios.interceptors.request.use(
  config => {
    //在发送请求之前做某件事
    if (config.method === "post") {
      config.data = QS.stringify(config.data);
    }
    return config;
  },
  error => {
    console.log("错误的传参");
    Toast("错误的传参");
    return Promise.reject(error);
  }
);

//返回状态判断(添加响应拦截器)
axios.interceptors.response.use(
  res => {
    //对响应数据做些事
    if (!res.data.success) {
      return Promise.resolve(res);
    }
    return res;
  },
  error => {
    console.log("网络异常");
    Toast("网络异常");
    return Promise.reject(error);
  }
);

////返回一个Promise(发送Delete请求)
export function fetchDelete(url, param) {
  return new Promise((resolve, reject) => {
    axios
      .delete(url, { params: param })
      .then(
        response => {
          resolve(response);
        },
        err => {
          reject(err);
          // 错误处理
          Toast(response.data.msg);
        }
      )
      .catch(error => {
        reject(error);
        let message = "请求失败！请检查网络";
        //错误返回
        if (error.response) message = error.response.data.message;
        Toast(message);
      });
  });
}

////返回一个Promise(发送put请求)
export function fetchPut(url, param) {
  return new Promise((resolve, reject) => {
    axios
      .put(url, { params: param })
      .then(
        response => {
          resolve(response);
        },
        err => {
          reject(err);
          // 错误处理
          Toast(response.data.msg);
        }
      )
      .catch(error => {
        reject(error);
        let message = "请求失败！请检查网络";
        //错误返回
        if (error.response) message = error.response.data.message;
        Toast(message);
      });
  });
}

////返回一个Promise(发送post请求)
export function fetchPost(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .post(url, params)
      .then(
        response => {
          resolve(response);
        },
        err => {
          reject(err);
          // 错误处理
          Toast(response.data.msg);
        }
      )
      .catch(error => {
        reject(error);
        let message = "请求失败！请检查网络";
        //错误返回
        if (error.response) message = error.response.data.message;
        Toast(message);
      });
  });
}

////返回一个Promise(发送get请求)
export function fetchGet(url, param) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, { params: param })
      .then(
        response => {
          resolve(response);
        },
        err => {
          reject(err);
          // 错误处理
          Toast(response.data.msg);
        }
      )
      .catch(error => {
        reject(error);
        let message = "请求失败！请检查网络";
        //错误返回
        if (error.response) message = error.response.data.message;
        Toast(message);
      });
  });
}

export default {
  fetchDelete,
  fetchPut,
  fetchPost,
  fetchGet
};
